#include "mq_connection.hpp"

void cb(ns_kkymq::Channel::ptr &channel, const std::string &tag,
        const ns_kkymq::BasicProperties *bp, const std::string &body)
{
    std::cout << tag << "消费了消息:" << body << std::endl;
    channel->basicAck(bp->id());
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        lg(ns_log::Debug, "usage: ./consume_client queue1");
        return -1;
    }
    // 1.实例化异步工作线程对象
    ns_kkymq::AsyncWorker::ptr awp = std::make_shared<ns_kkymq::AsyncWorker>();
    // 2.实例化连接对象
    ns_kkymq::Connection::ptr conn = std::make_shared<ns_kkymq::Connection>("127.0.0.1", 8085, awp);
    // 3.通过连接创建信道
    ns_kkymq::Channel::ptr channel = conn->openChannel();
    // 4.通过信道提供的服务完成所需
    // 4.1.声明一个交换机exchange1,交换机类型为广播模式
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1", ns_kkymq::ExchangeType::TOPIC, true, false, tmp_map);
    // 4.2.声明一个队列queue1
    channel->declareQueue("queue1", true, false, false, tmp_map);
    // 4.3.声明一个队列queue2
    channel->declareQueue("queue2", true, false, false, tmp_map);
    // 4.4.绑定queue1-exchange1，且binding_key设置为queue1
    bool ret1 = channel->queueBind("exchange1", "queue1", "queue1");
    // 4.5.绑定queue2-exchange1，且binding_key设置为news.music.#
    bool ret2 = channel->queueBind("exchange1", "queue2", "news.music.#");
    // std::cout<<"ret1: "<<ret1<<",ret2: "<<ret2<<std::endl;
    auto functor = std::bind(cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->basicConsume("consume1", argv[1], false, functor);
    // std::cout << "运行到这里来了嘛" << endl;
    while (1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }
    // 6.关闭信道
    conn->closeChannel(channel);
}